package persistence.util;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 
 * @author paul jaklitsch
 * 
 * Utility class for JdbcDAOImpls
 *
 */

public class JdbcUtil {	

	/*
	 * taken from Java Blueprints PetStore example 1.4.03
	 */
	/**
	 * properly closes DB resources
	 * 
	 */
	public static void cleanup(Connection con, Statement stmt, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			}
			catch (SQLException se) {
				se.printStackTrace();
			}
		}
		if (stmt != null) {
			try {
				stmt.close();
			}
			catch (SQLException se) {
				se.printStackTrace();
			}
		}
		if (con != null) {					
			try {
				con.close();
			}
			catch (SQLException se) {
				se.printStackTrace();
			}
		}
	}
	/**
	 * 
	 * @param stmt
	 * @param paramValue
	 * @param paramIndex
	 * @param sqlType
	 * @throws SQLException
	 * 
	 * Allows to set null values as PreparedStatement parameters
	 */
	public static void setParam(PreparedStatement stmt, Object paramValue, int paramIndex, int sqlType) throws SQLException{
		if (paramValue == null) {
			stmt.setNull(paramIndex, sqlType);
		}
		else {
			System.out.println("param value = " + paramValue);
			stmt.setObject(paramIndex, paramValue, sqlType);
		}
	}
}